package com.yjha.lzmarathonsys.controller;

//import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.yjha.lzmarathonsys.bean.TbLike;
import com.yjha.lzmarathonsys.service.TbLikeService;
import com.yjha.lzmarathonsys.utils.JwtUtils;
import com.yjha.lzmarathonsys.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 点赞记录表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2025-05-13 15:07:07
 */
@Api(tags = "点赞管理")
@RestController
@RequestMapping("lzmarathonsys/tblike")
// @CrossOrigin
public class TbLikeController {
    @Autowired
    private TbLikeService tbLikeService;


    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/save")
    @ApiOperation("点赞")
    public R save(@RequestBody TbLike tbLike , HttpServletRequest request) {

        return tbLikeService.like(tbLike,request);
    }

    @GetMapping("/check")
    @ApiOperation("检查当前用户是否点赞")
    public R checkLike(@RequestParam String targetId , HttpServletRequest request) {
        String tokenFromRequest = JwtUtils.getTokenFromRequest(request);

        String openId = JwtUtils.getOpenId(tokenFromRequest);

        String redisKey = "like:target:" + targetId;
        Boolean isMember = redisTemplate.opsForSet().isMember(redisKey, openId);

        return R.ok().put("liked", Boolean.TRUE.equals(isMember));
    }

    @GetMapping("/count")
    @ApiOperation("获取对应打卡点赞数")
    public R getLikeCount(@RequestParam String targetId) {
        return tbLikeService.getLikeCount(targetId);
    }
}
